﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.TipsCommands
{
    public class CheckTipSearchHandler : IRequestHandler<CheckTipSearchCommand, Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        private readonly IBaseRepository<Fields> fieldsRepository;

        public CheckTipSearchHandler(IBaseRepository<SleepInfor> sleepinforbaseRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<Cottages> cottagebaseRepository)
        {
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.fieldsRepository = fieldsRepository;
            this.cottagebaseRepository = cottagebaseRepository;
        }
        /// <summary>
        /// 业务逻辑（首次妊检提示）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(CheckTipSearchCommand request, CancellationToken cancellationToken)
        {
            var result= new Result() { Code=(int)CodeEnum.search};
            var list=(from a in sleepinforbaseRepository.GetAll()
                     join b in cottagebaseRepository.GetAll() on a.RecordcottageId equals b.CottagesId 
                     join c in fieldsRepository.GetAll() on a.FieIdId equals c.FieIdId
                     where a.Gender==2&&a.CheckPre==null
                     select new TipsDto()
                     {
                         Gender=a.Gender,
                         CheckPre=a.CheckPre,
                         EarNumber = a.EarNumber,
                         SleepEntryDate = a.SleepEntryDate,
                         CottageName=b.CottageName,
                         FieIdName=c.FieIdName,
                     }).ToList();
            result.Data = list;
            result.Count = list.Count;
            return result;
        }
    }
}
